const express = require("express")
const router = express.Router()
const db = require("../../sqlServer/dbConnect")
const time = require("../../utils/time")
const util = require("../../utils/index")

// 角色-- 查询
router.post("/info", (req, res) => {
    const { values, pageConfig, doName } = req.body;
    const { page, pageSize } = pageConfig
    let sql = values === null ?
        pageSize === undefined ? "select * from role;select * from role" : "select * from role limit ?,?;select * from role"
        : "select * from role where name=? limit ?,?;select * from role where name = ?"
    const data = values === null ?
        pageSize === undefined ? [] : [(Number(page - 1) * pageSize), pageSize]
        : [values.name, (Number(page - 1) * pageSize), pageSize, values.name]
    db.sqlQuery(sql, data, (err, roleRes) => {
        if (err) {
            console.log(err)
            res.send({
                // data: result,
                msg: "获取信息失败",
                status: 203
            })
        } else {
            db.sqlQuery("select * from menu", (err, result) => {
                if (err) { console.log(err) }
                else {
                    res.send({
                        data: roleRes[0],
                        power: util.arrayToTree(result),
                        msg: "获取成功",
                        total: roleRes[1].length,
                        status: 200
                    })
                }
            })
            powerList = []
        }
    })
    db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)", [doName, "角色查询", time.getData()])
})

// 角色-- 添加
router.post("/add", (req, res) => {
    let { name, power, btnPower, description, doName } = req.body
    const createtime = time.getData()
    power = `${power.join(",")}`
    btnPower = `${btnPower.join(",")}`
    let sql = "insert into role(name,power,btnPower,createtime,description) values(?,?,?,?,?)"
    const data = [name, power, btnPower, createtime, description]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "添加失败",
                status: 203
            })
        } else {
            res.send({
                msg: "添加成功",
                status: 200
            })
        }
    })
    db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)", [doName, "角色添加", time.getData()])
})

// 角色-- 编辑
router.post("/edit", (req, res) => {
    let { name, power, btnPower, description, Id, doName } = req.body
    power = `${power.join(",")}`
    btnPower = `${btnPower.join(",")}`
    let sql = "update role set name=?,power=?,btnPower=?,description=? where Id=?"
    const data = [name, power, btnPower, description, Id]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "编辑失败",
                status: 203
            })
        } else {
            res.send({
                msg: "编辑成功",
                status: 200
            })
        }
    })
    db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)", [doName, "角色编辑", time.getData()])
})

// 角色-- 删除
router.post("/delete", (req, res) => {
    const { Id, role, doName } = req.body
    let sql = "delete from role where Id=?"
    const data = [Id]
    db.sqlQuery("select * from user where role=?", [role], (err, result) => {
        if (err) { console.log(err) }
        else {
            if (result.length >= 1) {
                res.send({ msg: "该角色有用户在使用，删除失败", status: 203 })
            } else {
                db.sqlQuery(sql, data, (err) => {
                    if (err) {
                        console.log(err)
                        res.send({
                            msg: "删除失败",
                            status: 203
                        })
                    } else {
                        res.send({
                            msg: "删除成功",
                            status: 200
                        })
                    }
                })
            }
        }
    })
    db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)", [doName, "角色删除", time.getData()])
})

// 获取所有权限按钮
router.post("/allBtn", (req, res) => {
    db.sqlQuery("select * from power", (err, result) => {
        if (err) { console.log(err) }
        else {
            res.send({
                power: util.arrayToTree(result),
                msg: "获取成功",
                status: 200
            })
        }
    })
})
module.exports = router